package com.wmh.baseservice.operationaid.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wmh.baseservice.common.ex.ServiceException;
import com.wmh.baseservice.common.mybatisplus.structure.LambdaQueryWrapperX;
import com.wmh.baseservice.common.utils.web.UserUtil;
import com.wmh.baseservice.operationaid.entity.FeatureParamTemplate;
import com.wmh.baseservice.operationaid.enums.FeatureType;
import com.wmh.baseservice.operationaid.mapper.FeatureParamTemplateMapper;
import com.wmh.baseservice.operationaid.pojo.vo.FeatureParamTemplate.CommitReqVO;
import com.wmh.baseservice.operationaid.pojo.vo.FeatureParamTemplate.TableRepsVO;
import com.wmh.baseservice.operationaid.service.FeatureParamTemplateService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author 谢骏
 * @description 针对表【operation_aid_feature_param_template(运营助手-功能参数模板)】的数据库操作Service实现
 * @createDate 2023-07-03 14:29:15
 */
@Service
public class FeatureParamTemplateServiceImpl extends ServiceImpl<FeatureParamTemplateMapper, FeatureParamTemplate>
        implements FeatureParamTemplateService {

    @Resource
    private UserUtil userUtil;

    @Override
    public Long commit(CommitReqVO reqVO) {
        Long adId = userUtil.getId();
        checkTemplateUniqueName(reqVO, adId);
        FeatureParamTemplate template = new FeatureParamTemplate();
        template.setAdId(adId).setName(reqVO.getName())
                .setParams(reqVO.getJsonParams()).setType(reqVO.getType().getValue())
                .setId(reqVO.getId());
        if (!saveOrUpdate(template)) {
            throw new ServiceException("提交模板失败");
        }
        return template.getAdId();
    }

    @Override
    public void delete(List<Long> ids) {
        remove(new LambdaQueryWrapperX<FeatureParamTemplate>()
                .in(FeatureParamTemplate::getId, ids)
                .eq(FeatureParamTemplate::getAdId,userUtil.getId()));
    }

    @Override
    public List<TableRepsVO> table(FeatureType type) {
        return TableRepsVO.build(baseMapper.selectList(new LambdaQueryWrapperX<FeatureParamTemplate>()
                .eq(FeatureParamTemplate::getType, type.getValue())
                .eq(FeatureParamTemplate::getAdId, userUtil.getId())));
    }


    /**
     * 校验模板名称唯一性
     *
     * @param reqVO 请求体
     */
    private void checkTemplateUniqueName(CommitReqVO reqVO, Long adId) {
        // 同用户 同类型 同名 不同主键
        FeatureParamTemplate template = baseMapper.selectOne(new LambdaQueryWrapperX<FeatureParamTemplate>()
                .eq(FeatureParamTemplate::getAdId, adId)
                .eq(FeatureParamTemplate::getType, reqVO.getType().getValue())
                .eq(FeatureParamTemplate::getName, reqVO.getName())
                .neIfPresent(FeatureParamTemplate::getId, reqVO.getId()));
        if (!Objects.isNull(template)) {
            throw new ServiceException("模板名称重复");
        }
    }
}




